﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;

namespace Automata.examen2
{
    public class AlgoritmoBusqueda
    {
        private ArrayList list;

        private Boolean ordered;

        public AlgoritmoBusqueda()
        {
            this.list = null;
            this.ordered = false;
        }

        public ArrayList LIST
        {
            get { return list; }
        }

        public Boolean ORDERED
        {
            get { return ordered; }
        }

        public void cargaLista(Stream archivo)
        {
            string contenido = EntradaSalida.leeArchivo(archivo);
            ArrayList numberList = new ArrayList();

            Boolean orderCheck = true;
            int? lastNumber = null;

            try
            {
                string[] numeros = contenido.Split(new char[] { ' ' });

                foreach (string numero in numeros)
                {
                    int currentNumber = Int32.Parse(numero);

                    if (lastNumber == null)
                    {
                        lastNumber = currentNumber;
                    }
                    else if (currentNumber < lastNumber)
                    {
                        orderCheck = false;
                    }
                    numberList.Add(currentNumber);
                }
            }
            catch (Exception)
            {
                this.list = null;
                this.ordered = false;
                return ;
            }

            this.list = numberList;
            this.ordered = orderCheck;
        }

        public string bruteForce(int searchValue)
        {
            int totalComparations = 0;
            foreach (int number in this.LIST)
            {
                if (number == searchValue)
                {
                    totalComparations++;
                    return "Se ha encontrado el valor " + searchValue + "<br />Operaciones realizadas: " + totalComparations;
                }
                totalComparations++;
            }
            return "No se ha encontrado el valor " + searchValue + "<br />Operaciones realizadas: " + totalComparations;
        }

        public string binarySearch(int searchValue)
        {
            return recursiveBinarySearch(searchValue, 0, this.LIST.Count - 1, 1);
        }

        private string recursiveBinarySearch(int searchValue, int low, int high, int totalComparisons)
        {
            if (high < low)
            {
                return "No se ha encontrado el valor " + searchValue + "<br />Operaciones realizadas: " + totalComparisons;
            }
            int mid = low + (high - low) / 2;
            if ((int)this.LIST[mid] > searchValue)
            {
                return recursiveBinarySearch(searchValue, low, mid - 1, totalComparisons + 1);
            }
            else if ((int)this.LIST[mid] < searchValue)
            {
                return recursiveBinarySearch(searchValue, mid + 1, high, totalComparisons + 1);
            }
            else
            {
                return "Se ha encontrado el valor " + searchValue + "<br />Operaciones realizadas: " + totalComparisons;
            }
        }
    }
}